package com.openflag.bwicservice.controller;

import com.openflag.bwicservice.domain.*;
import com.openflag.bwicservice.exception.BwicOverdueException;
import com.openflag.bwicservice.exception.UserDidNotBidException;
import com.openflag.bwicservice.repository.BidRepository;
import com.openflag.bwicservice.util.ResultUtil;
import com.openflag.bwicservice.domain.vo.BwicVO;
import com.openflag.bwicservice.service.BwicService;
import com.openflag.bwicservice.service.LoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static com.openflag.bwicservice.util.ResultUtil.fail;
import static com.openflag.bwicservice.util.ResultUtil.success;
import static java.util.Arrays.asList;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/bwic")
public class BwicController {
    private Logger LOG = LoggerFactory.getLogger(BwicController.class);

    @Autowired
    private BwicService bwicService;
    @Autowired
    private LoginService loginService;
    @Autowired
    private BidRepository bidRepository;

    @GetMapping("/list/{clientId}")
    public Result<List<BwicVO>> list(@PathVariable Long clientId) {
        // list bwics
        // if client bid: show bid rank, bid market value
        // if overdue: show winner
        List<BwicVO> result = bwicService.list(clientId);
        LOG.info("Get list: {} by client id: {}", result, clientId);
        return success(result);
    }

    @GetMapping("/mylist/{clientId}")
    public Result<List<BwicVO>> mylist(@PathVariable Long clientId) {
        List<BwicVO> result = bwicService.mylist(clientId);
        LOG.info("Get mylist: {} by client id: {}", result, clientId);
        return success(result);
    }

    @PostMapping("/bid")
    public Result<List<BwicVO>> bid(@RequestBody BidRequest bidRequest) {
        Long clientId = bidRequest.getClientId();
        Client client = loginService.getClient(clientId);

        Result<List<BwicVO>> result = null;
        if(bidRequest.getBidMarketValue() > client.getAssetValue()) {
            result = fail(ResultStatus.USER_ASSET_NOT_ENOUGH);
        } else {
            List<BwicVO> bwicVOList = bwicService.bid(bidRequest);
            if(bwicVOList != null){
                result = success(bwicVOList);
            }else {
                result = fail(ResultStatus.USER_MARKETVALUE_TOO_LOW);
            }

        }

        LOG.info("Get result: {}, by Bid request: {}", result, bidRequest);
        return result;
    }

    @PostMapping("/cancel")
    public Result<List<BwicVO>> cancel(@RequestBody CancelRequest cancelRequest) {
        // if overdue
        Result<List<BwicVO>> result = null;
        try {
            result = success(bwicService.cancel(cancelRequest));
        } catch (BwicOverdueException overdueException) {
            result = fail(ResultStatus.BWIC_OVERDUE);
        } catch (UserDidNotBidException notBidException) {
            result = fail(ResultStatus.USER_DID_NOT_BID);
        }

        LOG.info("Get result: {}, by Cancel request: {}", result, cancelRequest);
        return result;
    }

    @GetMapping("/top3list/{bwicId}")
    public Result<List<Client>> top3List(@PathVariable Long bwicId){


        List<Client> clientList = bwicService.top3list(bwicId);

        return success(clientList);





    }


    private List<BwicVO> mockList() {
        BwicVO bwic1 = new BwicVO();
        bwic1.setBwicId(1L);
        bwic1.setCusip("037833100");
        bwic1.setPosition(400L);
        bwic1.setPrice(30.0);
        bwic1.setMarketValue(10000L);
        bwic1.setDueDate(LocalDate.of(2023, 2, 20));

        bwic1.setOverDue(LocalDate.now().isAfter(bwic1.getDueDate()));
        bwic1.setTotalBids(10);
        bwic1.setBidRank("3/5");
        bwic1.setBidMarketValue(10500L);
        if(bwic1.getOverDue()) {
            bwic1.setWinner("Alpha");
        }

        BwicVO bwic2 = new BwicVO();
        bwic2.setBwicId(2L);
        bwic2.setCusip("02079K107");
        bwic2.setPosition(80L);
        bwic2.setPrice(10.0);
        bwic2.setMarketValue(500L);
        bwic2.setDueDate(LocalDate.of(2023, 8, 20));

        bwic2.setOverDue(LocalDate.now().isAfter(bwic2.getDueDate()));
        bwic2.setTotalBids(8);
        bwic2.setBidRank("5/10");
        bwic2.setBidMarketValue(450L);
        if(bwic2.getOverDue()) {
            bwic2.setWinner("Beta");
        }

        return asList(bwic1, bwic2);
    }
}
